package dataAccess;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.LinkedList;

import dataType.AgencyData;

import exception.MyException;
import logic.Agency;

public class AgencyDB {
	
	public void save(Agency ag) throws MyException {
		DBManager db = null;
		try {
			db = new DBManager();
		} catch (MyException e1) {
			throw e1;
		}
		try {			
			String aux = "insert into imm.agency (name, userCode, userPwd)values ('"+ag.getName()+"','"+ag.getUser()+"','"+ag.getPassword()+"') ";
			db.execute(aux);
		} catch (MyException e) {
			throw e;
			
		}finally{
			db.close();
		}
	}
	
	public void update(Agency ag) throws MyException{
		DBManager db = null;
		try {
			db = new DBManager();
		}catch (MyException e1){
			throw e1;
		}
		try {
			String update = "UPDATE imm.agency set name = '"+ ag.getName()+"'"
						+ ", userCode = '" + ag.getUser()+"'"
						+ ", userPwd = '" + ag.getPassword() +"'"
						+ " where idAgency = "+ag.getIdAgency();
			db.execute(update);
		} catch (MyException e){
			throw e;
		}finally{
			db.close();
		}
	}	
	
	public void delete(Agency ag){
		
	}
	
	public Agency find(int id) throws MyException{
		DBManager db = null;
		try {
			db = new DBManager();
		} catch (MyException e1) {
			throw e1;
		}
		Agency a = null;
		String select = "SELECT name,userCode,userPwd FROM imm.agency where idAgency = " + id;
		try {
			ResultSet rs = db.executeAndReturn(select);
			if (rs != null){
				rs.next();
				String name = rs.getString(1);
				String userCode = rs.getString(2);
				String userPwd = rs.getString(3);
				a = new Agency(id,name,userCode,userPwd);
			}	
		} catch (SQLException e) {
			throw new MyException("Error en la Base datos al recorrer registro.");
		}catch (MyException e) {
			throw e;
		}finally{
			db.close();
		}		
		return a; 
	}
	
	public float getSales(){
		return -1;
	}
	
	public boolean isFromAgency(){
		return false;
	}
	
/*	public DTTicket generateDTTicketFromRS(ResultSet rs){
		
	}
*/
		
	public Collection<Agency> find(){
		return null;
	}
	
	@SuppressWarnings("finally")
	public Collection<AgencyData> findall() throws MyException{
		DBManager db = null;
		Collection<AgencyData> col = null;
		try {
			db = new DBManager();
		} catch (MyException e) {
			throw e;
		}
		try {
			String select = "SELECT * FROM imm.agency";
			ResultSet rs;			
			rs = db.executeAndReturn(select);
			col = new LinkedList<AgencyData>();
			while (rs.next()){
				int idAgency = rs.getInt("idAgency");
				String name = rs.getString("name");
				String userCode = rs.getString("userCode");
				String userPwd = rs.getString("userPwd");
					
				AgencyData dtag = new AgencyData(idAgency,name,userCode,userPwd);
				col.add(dtag);
			}
		} catch (SQLException e1) {
			throw new MyException("Error en la Base datos al recorrer registro.");
		} catch (MyException e2) {
			throw e2;
		}finally{
			db.close();
			return col;
		}	
	}
}
